package admin

import (
	"easyblog/logicals"
	"easyblog/models"
	"easyblog/utils/errmsg"
	"encoding/json"
	"github.com/beego/beego/v2/core/validation"
	"github.com/beego/beego/v2/server/web"
)

type CategoryController struct {
	web.Controller
}

// @router /admin/category [post]
func (this *CategoryController)AddCategory (){
	var category models.Category
	body := this.Ctx.Input.RequestBody
	json.Unmarshal(body,&category)

	result := make(map[string]interface{})

	valid := validation.Validation{}
	b, _ := valid.Valid(&category)
	if !b {
		for _, err := range valid.Errors {
			result["status"] = errmsg.ERROR
			result["message"] = err.Key + " " + err.Message
			this.Data["json"] = result
			this.ServeJSON()
			this.StopRun()
		}
	}

	code := logicals.DefaultCategory.CheckCategoryExists(category.Name)
	if code != errmsg.ERROR_CATEGORYNAME_USED{
		code = logicals.DefaultCategory.CreateCategory(&category)
	}
	result["status"] = code
	result["message"] = errmsg.GetErrmsg(code)
	this.Data["json"] = result
	this.ServeJSON()
}

// @router /admin/category/:id [put]
func (this *CategoryController)EditCategory (){
	var category models.Category
	body := this.Ctx.Input.RequestBody
	_ = json.Unmarshal(body,&category)
	id, _ := this.GetInt(":id")

	result := make(map[string]interface{})

	valid := validation.Validation{}
	b, _ := valid.Valid(&category)
	if !b {
		for _, err := range valid.Errors {
			result["status"] = errmsg.ERROR
			result["message"] = err.Key + " " + err.Message
			this.Data["json"] = result
			this.ServeJSON()
			this.StopRun()
		}
	}

	code := logicals.DefaultCategory.CheckCategoryExists(category.Name)
	if code != errmsg.ERROR_CATEGORYNAME_USED{
		code = logicals.DefaultCategory.EditCategory(id,&category)
	}
	result["status"] = code
	result["message"] = errmsg.GetErrmsg(code)
	this.Data["json"] = result
	this.ServeJSON()
}

// @router /admin/category/:id [delete]
func (this *CategoryController)DeleteCategory (){
	id, _ := this.GetInt(":id")

	code := logicals.DefaultCategory.DeleteCategory(id)
	result := map[string]interface{}{
		"status": code,
		"message": errmsg.GetErrmsg(code),
	}
	this.Data["json"] = result
	this.ServeJSON()
}

// @router /admin/category/:id [get]
func (this *CategoryController)GetOneCategory (){
	id, _ := this.GetInt(":id")

	category, code := logicals.DefaultCategory.GetCategoryById(id)
	result := map[string]interface{}{
		"status": code,
		"data":category,
		"message": errmsg.GetErrmsg(code),
	}
	this.Data["json"] = result
	this.ServeJSON()
}

// @router /admin/categories [get]
func (this *CategoryController) GetPageCategories() {
	pageSize, err := this.GetInt("pagesize")
	if pageSize < 1 || err != nil{
		pageSize = 5
	}
	pageNum, err := this.GetInt("pagenum")
	if pageNum < 1 || err != nil{
		pageNum = 1
	}

	data, total := logicals.DefaultCategory.GetPageCategories(pageSize, pageNum)
	code := errmsg.SUCCSE

	result := map[string]interface{}{
		"status":  code,
		"data":    data,
		"total":   total,
		"message": errmsg.GetErrmsg(code),
	}
	this.Data["json"] = result

	this.ServeJSON() //返回json格式
}


